package com.jacoblyu.factoryrunner.data

import android.content.Context
import android.content.SharedPreferences

class TestResultManager(context: Context) {
    private val sharedPreferences: SharedPreferences = 
        context.getSharedPreferences("TestResults", Context.MODE_PRIVATE)
    
    companion object {
        private const val TEST_RESULT_PREFIX = "test_result_"
        
        // 测试项名称列表
        val TEST_ITEMS = listOf(
            "USB测试",
            "屏幕测试",
            "麦克风喇叭测试",
            "Wi-Fi测试",
            "蓝牙测试",
            "照明测试",
            "前置摄像头测试",
            "高拍摄像头测试",
            "TF卡测试"
        )
    }
    
    // 保存测试结果
    fun saveTestResult(testName: String, result: String) {
        sharedPreferences.edit()
            .putString(TEST_RESULT_PREFIX + testName, result)
            .apply()
    }
    
    // 获取测试结果
    fun getTestResult(testName: String): String {
        return sharedPreferences.getString(TEST_RESULT_PREFIX + testName, "未测试") ?: "未测试"
    }
    
    // 获取所有测试结果
    fun getAllTestResults(): Map<String, String> {
        val results = mutableMapOf<String, String>()
        TEST_ITEMS.forEach { testName ->
            results[testName] = getTestResult(testName)
        }
        return results
    }
    
    // 清除所有测试结果
    fun clearAllResults() {
        val editor = sharedPreferences.edit()
        TEST_ITEMS.forEach { testName ->
            editor.remove(TEST_RESULT_PREFIX + testName)
        }
        editor.apply()
    }
    
    // 清除单个测试结果
    fun clearTestResult(testName: String) {
        sharedPreferences.edit()
            .remove(TEST_RESULT_PREFIX + testName)
            .apply()
    }
}